package com.xingxuecn.babatis02.common;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * 容器，放置项目中需要使用的单列实例
 * @author Kris at 2018/9/21 9:37
 */
public class ContextContainer {

    private static SqlSessionFactory sqlSessionFactory;

    /**
     * 获取mybatis的SqlSessionFactory对象
     * @return
     */
    public static SqlSessionFactory getSqlSessionFactory() {
        if(sqlSessionFactory == null){
            InputStream inputStream = ContextContainer.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }
        return sqlSessionFactory;
    }
}
